package com.yd.mvc.pc.agentController;

import com.yd.app.service.pc.agentService.AgentService;
import com.yd.app.service.pc.agreementService.AgreementService;
import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.Agreement;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.DicEnum;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yd.domain.sys.Agent;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by zg on 2018/3/9.
 */
@RestController
public class AgentController extends BaseController {
    @Autowired
    AgentService agentService;
    @Autowired
    UserService userService;

    /**查询列表
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/dapi/agent/getList", method = RequestMethod.GET)
    public List<Agent> getAgentList() throws BusinessException {
        List<Agent> list = null;
        try {
            list = agentService.getAgentList();
        } catch (Exception e) {
            throw new BusinessException(500, "获取代理商信息失败");
        }
        return list;
    }

    /**
     * 删除代理信息
     * @param agentId
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/agent/delete/{agentId}", method = RequestMethod.DELETE)
    public void deleteAgentById(@PathVariable("agentId")String agentId) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGENT_MANAGER);//验证权限
        }
        int count=userService.getUserByAgentId(agentId);
        if(count>0){
            throw new BusinessException(500, "该代理商已有注册用户不可删除");
        }else{
            agentService.deleteAgentById(agentId);
        }
    }

    /**
     * 添加代理商
     * @param agent
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/agent/add", method = RequestMethod.POST)
    public void addAgent(@RequestBody Agent agent) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGENT_MANAGER);//验证权限
        }
        agentService.addAgent(agent);
    }
    /**
     * 修改代理商
     * @param agent
     * @throws BusinessException
     */
    @RequestMapping(value = "/api/agent/update", method = RequestMethod.POST)
    public void updateAgent(@RequestBody Agent agent) throws BusinessException {
        AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
        if(user!=null){
            getIfHasPerm(AuthUtil.getCurrentUser(), DicEnum.PERM_CODE.AGENT_MANAGER);//验证权限
        }
        agentService.updateAgent(agent);
    }
    /**
     * 获取登录用户所注册的代理
     * @param session
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/mapi/agent/getAgentByUser", method = RequestMethod.GET)
    public Agent getAgentByUser(HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        Agent agent=agentService.getAgentByUser(u.getId());
        return agent;
    }

}
